/************************************************************************/
/*																		*/
/*  MAIN.CPP	--  DPCDEMO Main Program								*/
/*																		*/
/************************************************************************/
/*  Author:	Joshua Pederson												*/
/*  Copyright:	2004 Digilent, Inc.										*/
/************************************************************************/
/*  Module Description: 												*/
/*		DPCDEMO is meant only for demonstration of the DPCUTIL API		*/
/*		data transfer calls.  Before using this program, the FPGA on a	*/
/*		system board must be loaded with a logic design similar to that */
/*		of dpimref.vhd (found on the Digilent website).					*/
/*																		*/
/*		Note:  the error checking in DPCDEMO is minimal in an effort to	*/
/*		simplify the code in the application.							*/
/*																		*/
/************************************************************************/
/*  Revision History:													*/
/*																		*/
/*	08/20/2004(Josh): created											*/
/*	09/20/2004(Josh): prepared for release

/************************************************************************/

/* ------------------------------------------------------------ */
/*					Include File Definitions					*/
/* ------------------------------------------------------------ */

#include <windows.h>
#include <stdio.h>



#include "dpcdefs.h"	/* holds error codes and data types for dpcutil	*/
#include <dpcutil.h>	/* holds declaration of DPCUTIL API calls		*/
#include <gendefs.h>

// Open CV
#include "cv.h"
#include <highgui.h>


#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>


/* retorna "a - b" en segundos */



/* ------------------------------------------------------------ */
/*					Local Type Definitions						*/
/* ------------------------------------------------------------ */


/* ------------------------------------------------------------ */
/*					Global Variables							*/
/* ------------------------------------------------------------ */


/* ------------------------------------------------------------ */
/*					Local Variables								*/
/* ------------------------------------------------------------ */

/* The following variables hold flags and parameters from the command
** line.
*/

BOOL			fGetReg;
BOOL			fPutReg;
BOOL			fGetRegRepeat;
BOOL			fPutRegRepeat;
BOOL			fFirstParam;
BOOL			fSecondParam;
BOOL			fDvctbl;
BOOL			fDefDvc;
BOOL 			fPromedio;
BOOL			fSacaImagen;

char			szFirstParam[cchShortString+1];
char			szSecondParam[MAX_PATH+1];
char			szThirdParam[cchShortString+1];
char			szDefDvc[cchShortString+1];





/* ------------------------------------------------------------ */
/*					Forward Declarations						*/
/* ------------------------------------------------------------ */

static BOOL		FProcessCommandLine(int argc, char * argv[]);
static void		ShowUseage(char * sz);
static BOOL		FInit();

static void		DoDvcTbl();
static void		DoPutReg();
static int		DoGetReg();
static void		DoPutRegRepeat();
static double		DoGetRegRepeat();
static void		SacaImagen();

/* ------------------------------------------------------------ */

/***	main
**
**	Synopsis
**		void main(argc, argv)
**
**	Input:
**		argc		- count of command line arguments
**		argv		- array of command line arguments
**
**	Output:
**		none
**
**	Errors:
**		Exits with exit code 0 if successful, else non-zero
**
**	Description:
**		main function of DPCDEMO application.
**
*/


// Funciones




int main(){

using namespace std;

bool result;
ERC erc;
int idDvc;


////////////////// fichero de log
	FILE *	fhout;
	fhout = fopen("log.txt", "wb");
	fprintf( fhout,"Esto es un ejemplo usando \x0D\x0A" );
////////////////////////////////

	if (!FInit()) {
			fputs("An error occured while initializing the application \x0D\x0A",fhout);
			goto lExit;
		}


	long	cb;
	BYTE	idReg;
	char *	szStop;
	BYTE	rgbStf[cbBlockSize];
	int		cbGet, cbGetTotal;

	HANDLE	hif;



////////////////////////////////////////////////////////////////////////////Open Data
		/* DPCUTIL API CALL : DpcOpenData
		*/
		if (!DpcOpenData(&hif, szDefDvc, &erc, NULL)) {
			printf("DpcOpenData failed.\x0D\x0A");
			fputs("DpcOpenData failed.\x0D\x0A",  fhout);
			goto lExit;
					}
		if (hif == NULL) {
			printf("Cannot open device\x0D\x0A");
			fputs("Cannot open device\x0D\x0A",  fhout);
			goto lExit;
		}
		//
/////////////////////////////////////////////////////////////////////////////////////	open cv ini
	///////////////////////////////
	//Opencv
	static CvMemStorage* storage = 0;
	IplImage* img;
	uchar* dataA;
	img= cvCreateImage(cvSize(640, 240), 8, 1);
	dataA = (uchar *)img->imageData;
	int key;


//////////////////////////////////////////////////////////////////////////////////////////   peticion
	while (key!= 'q'){

	BYTE idData;
	int GR_c;
	GR_c=0;

	idReg = (BYTE) strtol("16", &szStop, 10);

	DpcGetReg(hif, idReg, &idData, &erc, NULL);
	fputs("entra en getReg \x0D\x0A",  fhout);
	while (idData < 127){
		GR_c++;
		fprintf(fhout," %i entra en el while getReg \x0D\x0A",GR_c);
		DpcGetReg(hif, idReg, &idData, &erc, NULL);

	}
/////////////////////////////////////////////////////////////////////////////////////////  cierre peticion

///////////////////////////////////////////////////////////////////////////////////////// get reg repeat


	idReg = (BYTE) strtol("13", &szStop, 10);
	int i,j;
	i=0;
	j=0;

		DpcGetRegRepeat(hif, idReg, rgbStf, cbBlockSize, &erc, NULL);
		fprintf(fhout," %i entra en el FOR \x0D\x0A",i);
			for ( j=0;j<cbBlockSize;j++){
/*
				if((int)rgbStf[j]=='1'){
					i++;
				}
*/
				dataA[i*639+j]=(int)rgbStf[j];
				if(!(j%639)){
					fprintf(fhout,"\x0D\x0A \x0D\x0A");
					}
				fprintf(fhout," %i",(int)rgbStf[j]);
			}
			fprintf(fhout,"\x0D\x0A %i Pixeles Recibidos\x0D\x0A",j);

	cvShowImage( "mi imagen", img);
	key = cvWaitKey( 1);
	//cvWaitKey();
}//Bucle while
	cvSaveImage("test.jpg",img);
	cvWaitKey();


///////////////////////////////////////////////////////////////////////////////////////// cierre get reg repeat
				erc = DpcGetFirstError(hif);

				if (erc == ercNoError) {
					/* DPCUTIL API CALL : DpcCloseData
					*/
					DpcCloseData(hif, &erc);
					goto lExit;
					//printf("Complete!  Data received = %d\n", idData);
				}
				else{
					/* DPCUTIL API CALL : DpcCloseData
					*/
					DpcCloseData(hif, &erc);
					fputs("An error occured while reading the register \x0D\x0A", fhout);
					goto lExit;
				}


////////////////////////////// cierre fichero
lExit:
				   if( !fclose(fhout) )
				      printf( "\nFichero cerrado\n" );
				   else
				   {
				      printf( "\nError: fichero NO CERRADO\n" );
				      return 1;
				   }

////////////////////////////////////////////

	return 0;
}

/***	DoGetReg
**
**	Synopsis
**		void DoGetReg()
**
**	Input:
**		none
**
**	Output:
**		none
**
**	Errors:
**		none
**
**	Description:
**		Gets a byte from, a specified register
**
*/

static int
DoGetReg() {

	BYTE	idReg;
	BYTE	idData;
	char *	szStop;

	ERC		erc;
	HANDLE	hif;

	//idReg = (BYTE) strtol(szFirstParam, &szStop, 10);
	idReg = (BYTE) strtol("16", &szStop, 10);

	/* DPCUTIL API CALL : DpcOpenData
	*	notice last parameter is set to NULL.  This is a blocking call
	*/
	if (!DpcOpenData(&hif, szDefDvc, &erc, NULL)) {
		printf("DpcOpenData failed.\n");
		goto lFail;
	}

	/* DPCUTIL API CALL : DpcGetReg
	*/
	if (!DpcGetReg(hif, idReg, &idData, &erc, NULL)) {
		DpcCloseData(hif,&erc);
		printf("DpcGetReg failed.\n");
		goto lFail;
	}

	/* DPCUTIL API CALL : DpcGetFirstError
	*/
	erc = DpcGetFirstError(hif);

	if (erc == ercNoError) {
		/* DPCUTIL API CALL : DpcCloseData
		*/
		DpcCloseData(hif, &erc);
		//printf("Complete!  Data received = %d\n", idData);
	}
	else{
		/* DPCUTIL API CALL : DpcCloseData
		*/
		DpcCloseData(hif, &erc);
		printf("An error occured while reading the register.\n", idData);
	}

lFail:
	return idData;

}

static BOOL
FInit() {

	ERC erc;
	int idDvc;

	/* Set up the command line argument defaults.
	*/
	fGetReg			= fFalse;
	fPutReg			= fFalse;
	fGetRegRepeat	= fFalse;
	fPutRegRepeat	= fFalse;
	fFirstParam		= fFalse;
	fDvctbl			= fFalse;
	fPromedio 		= fFalse;

	/* DPCUTIL API CALL : DpcInit
	*/
	if (!DpcInit(&erc)) {
		return fFalse;
	}

	/* DPCUTIL API CALL : DvmgGetDefaultDev
	*/
	idDvc = DvmgGetDefaultDev(&erc);
	if (idDvc == -1) {
		printf("No default device\n");
		fDefDvc = fFalse;
		return fTrue;
	}
	else {
		fDefDvc = fTrue;
		/* DPCUTIL API CALL : DvmgGetDevName
		*/
		DvmgGetDevName(idDvc, szDefDvc, &erc);
		strcpy(szDefDvc, szDefDvc);
		return fTrue;
	}
}

/* ------------------------------------------------------------ */

//



